<?php

	// connect to the database
	require 'db_init.php';

	// store the form data
	$email = stripslashes($_POST['email']);
	$title = stripslashes($_POST['title']);
	
	if (!check_email_address($email)) {
		echo "Alert! Invalid email address!";
		exit;
	}

	// lookup the info in the database
	$query = "SELECT id FROM user WHERE email='$email'" ;

	if(mysql_num_rows(mysql_query($query))){  /* email address is in the database */
		
		// return the associated id
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$userid = $row['id'];
		
	} else {  /* email address is not already in the database */

		// increment the user id count
		$query = 'UPDATE count SET usercount = LAST_INSERT_ID(usercount+1)';
		mysql_query($query);
		
		// and safely retrieve this value
		$query = 'SELECT LAST_INSERT_ID()';
		$userid = mysql_result( mysql_query($query), 0, 0);
		
		// then add the userid and email address as a record into the 'user' table
		$query= "INSERT INTO user VALUES ('$userid','".addslashes($email)."')";
		mysql_query($query) or die(mysql_error());

	}
	
	// increment the stitch id counter
	$query = 'UPDATE count SET stitchcount = LAST_INSERT_ID(stitchcount+1)';
	mysql_query($query);
	
	// and safely retrieve this value
	$query = 'SELECT LAST_INSERT_ID()';
	$stitchid = mysql_result( mysql_query($query), 0, 0);
	
	// update database with the future stitched image's location!
	$query = "INSERT INTO image VALUES ($stitchid, $userid, CURDATE(),'".addslashes($title)."', 0, '".addslashes($_SERVER['REMOTE_ADDR'])."' )";
	mysql_query($query);
	
	// close the connection to the database
	mysql_close();
function check_email_address($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}

                                    
?>

		<!-- needed so that first visible upload slot works on IE... -->
		<div style="display:none;">
			<form id="slot0" style="display:none;" action="scripts/upload.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : function(){ return startCallback(document.getElementById('slot0'));}, 'onComplete' : function(response){ return completeCallback(document.getElementById('slot0'), response);}})" enctype="multipart/form-data">
				<div><label for="file0">Upload Slot 0: </label> <input type="file" name="userfile" id="file0" />
				<input type="hidden" name="stitchid" value="<?php echo $stitchid ?>" />
				<input type="submit" name="button" value="Upload" /></div>
			</form>
		</div>

		<form id="slot1" action="scripts/upload.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : function(){ return startCallback(document.getElementById('slot1'));}, 'onComplete' : function(response){ return completeCallback(document.getElementById('slot1'), response);}})" enctype="multipart/form-data">
			<div><label for="file1">Upload Slot 1: </label> <input type="file" name="userfile" id="file1" />
			<input type="hidden" name="stitchid" value="<?php echo $stitchid ?>" />
			<input type="submit" name="button" value="Upload" /></div>
		</form>
		
		<form id="slot2" action="scripts/upload.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : function(){ return startCallback(document.getElementById('slot2'));}, 'onComplete' : function(response){ return completeCallback(document.getElementById('slot2'), response);}})" enctype="multipart/form-data">
			<div><label for="file2">Upload Slot 2: </label> <input type="file" name="userfile" id="file2" />
			<input type="hidden" name="stitchid" value="<?php echo $stitchid ?>" />
			<input type="submit" name="button" value="Upload" /></div>
		</form>
		
		<form id="slot3" action="scripts/upload.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : function(){ return startCallback(document.getElementById('slot3'));}, 'onComplete' : function(response){ return completeCallback(document.getElementById('slot3'), response);}})" enctype="multipart/form-data">
			<div><label for="file3">Upload Slot 3: </label> <input type="file" name="userfile" id="file3" />
			<input type="hidden" name="stitchid" value="<?php echo $stitchid ?>" />
			<input type="submit" name="button" value="Upload" /></div>
		</form>
		
		<form id="slot4" action="scripts/upload.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : function(){ return startCallback(document.getElementById('slot4'));}, 'onComplete' : function(response){ return completeCallback(document.getElementById('slot4'), response);}})" enctype="multipart/form-data">
			<div><label for="file4">Upload Slot 4: </label> <input type="file" name="userfile" id="file4" />
			<input type="hidden" name="stitchid" value="<?php echo $stitchid ?>" />
			<input type="submit" name="button" value="Upload" /></div>
		</form>
		
		<h2>Step 3. Tag and stitch your panorama!</h2>
		
		<form name="tagform" action="stitch.php" method="post" enctype="multipart/form-data">
			<label for="title">Tags: </label>
			<input type="text" name="tags" id="tags" />
			<input type="hidden" name="stitchid" value="<?php echo $stitchid ?>" />
			<input type="submit" value="Stitch!" />
		</form>
		
		<div><p>Thumbnails:</p><p><span id="thumbs"></span></p></div>
